<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="姓名" maxlength="10" class="col"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="item in sexList"
            :key="item.data"
            :label="item.data"
            :value="item.data">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" placeholder="手机号" maxlength="11" class="col"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCards">
        <el-input v-model="form.idCards" placeholder="身份证号" maxlength="18" class="col"></el-input>
      </el-form-item>
      <el-form-item label="毕业院校" prop="school">
        <el-input v-model="form.school" placeholder="毕业院校" maxlength="15" class="col"></el-input>
      </el-form-item>
      <el-form-item label="毕业时间" prop="graduationTime">
        <el-input v-model="form.graduationTime" placeholder="毕业时间" maxlength="15" class="col"></el-input>
      </el-form-item>
      <el-form-item label="毕业证" prop="graduationUrl">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <el-image
            v-if="form.graduationUrl"
            referrerpolicy="no-referrer"
            style="width: 180px!important; height: 140px!important;"
            :src="form.graduationUrl"
            fit="contain"
            :preview-src-list="[form.graduationUrl]"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="银行账号" prop="bankNum">
        <el-input v-model="form.bankNum" placeholder="银行账号" maxlength="19" class="col"></el-input>
      </el-form-item>
      <el-form-item label="开户行" prop="bank">
        <el-input v-model="form.bank" placeholder="开户行" maxlength="15" class="col"></el-input>
      </el-form-item>
      <el-form-item label="企业名称" prop="ename">
        <el-input v-model="form.ename" placeholder="企业名称" maxlength="20"
                  :disabled="true" class="col"></el-input>
      </el-form-item>
      <el-form-item label="纳税人识别号" prop="taxpayerIdentificationNumber" v-if="form.taxpayerIdentificationNumber">
        <el-input v-model="form.taxpayerIdentificationNumber" placeholder="纳税人识别号" maxlength="20"
                  :disabled="true" class="col"></el-input>
      </el-form-item>
      <el-form-item label="所在部门" prop="department">
        <el-input v-model="form.department" placeholder="所在部门" maxlength="15" class="col"></el-input>
      </el-form-item>
      <el-form-item label="岗位名称" prop="postName">
        <el-input v-model="form.postName" placeholder="岗位名称" maxlength="15" class="col"></el-input>
      </el-form-item>
      <el-form-item label="企业联系电话" prop="ephone">
        <el-input v-model="form.ephone" placeholder="企业联系电话" maxlength="11" class="col"></el-input>
      </el-form-item>
      <el-form-item label="是否全勤" prop="fullAttendance">
        <el-select v-model="form.fullAttendance" placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="item in fullAttendanceList"
            :key="item.data"
            :label="item.data"
            :value="item.data">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="在职证明" prop="proofOfMployment">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess1"
          :before-upload="beforeAvatarUpload">
          <el-image
            v-if="form.proofOfMployment"
            referrerpolicy="no-referrer"
            style="width: 180px!important; height: 140px!important;"
            :src="form.proofOfMployment"
            fit="contain"
            :preview-src-list="[form.proofOfMployment]"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="参保证明" prop="proofInsuranceParticipation">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess2"
          :before-upload="beforeAvatarUpload">
          <el-image
            v-if="form.proofInsuranceParticipation"
            referrerpolicy="no-referrer"
            style="width: 180px!important; height: 140px!important;"
            :src="form.proofInsuranceParticipation"
            fit="contain"
            :preview-src-list="[form.proofInsuranceParticipation]"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="考勤明细" prop="attendanceEtails">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess3"
          :before-upload="beforeAvatarUpload">
          <el-image
            v-if="form.attendanceEtails"
            referrerpolicy="no-referrer"
            style="width: 180px!important; height: 140px!important;"
            :src="form.attendanceEtails"
            fit="contain"
            :preview-src-list="[form.attendanceEtails]"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary"
                   @click="handlerSubmit">保存
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {detail, getSelectEnterpriseById, save} from "@/api/enjoyEnterpriseSubsidies.js";
import {getForm} from "@/utils/utils";
import {getComListApi} from "@/api/livePortend";
import Cookies from "js-cookie";

export default {
  components: {},
  props: {
    id: {
      default: null
    }
  },
  data() {
    return {
      form: {
        id: null,
        name: '',
        sex: '',
        phone: '',
        idCards: '',
        school: '',
        graduationTime: '',
        graduationUrl: '',
        bankNum: '',
        bank: '',
        ename: '',
        eid: null,
        taxpayerIdentificationNumber: '',
        department: '',
        postName: '',
        ephone: '',
        fullAttendance: '',
        proofOfMployment: '',
        proofInsuranceParticipation: '',
        attendanceEtails: '',
      },
      enterpriseForm: {
        id: null,
        enterpriseName: '',
        erterpriseIdNum: '',
      },
      uploadUrl: process.env.VUE_APP_UPLOAD_API,
      rules: {},
      roles: [],
      comList: {},
      saveLoading: false,
      formLoading: false,
      sexList: [{id: 1, data: '男'}, {id: 2, data: '女'}],
      fullAttendanceList: [{id: 1, data: '是'}, {id: 2, data: '否'}],
      enterpriseId: null,
    }
  },
  mounted() {
    this.enterpriseId = JSON.parse(Cookies.get("user"))?.enterpriseId || null;
    if (this.id) {
      this.getDetail();
    }
    this.getEnterpriseDetail();
  },
  methods: {
    getEnterpriseDetail() {
      this.formLoading = true
      getSelectEnterpriseById(this.enterpriseId).then(res => {
        getForm(res, this.enterpriseForm);
        this.form.ename = this.enterpriseForm.enterpriseName
        this.form.taxpayerIdentificationNumber = this.enterpriseForm.erterpriseIdNum
        this.form.eid = this.enterpriseForm.id
      }).finally(() => {
        this.formLoading = false;
      })
    },
    getDetail() {
      this.formLoading = true
      detail(this.id).then(res => {
        getForm(res, this.form);
      }).finally(() => {
        this.formLoading = false;
      })
    },
    handlerSubmit() {
      if (this.saveLoading || this.formLoading) {
        return;
      }
      this.$refs.form.validate(valid => {
        if (!valid) return;
        this.saveLoading = true;
        save(this.form).then(() => {
          this.$message.success('保存成功');
          this.close();
        }).finally(() => {
          this.saveLoading = false
        })
      })
    },
    handleAvatarSuccess(res, file) {
      this.form.graduationUrl = res.data;
    },
    handleAvatarSuccess1(res, file) {
      this.form.proofOfMployment = res.data;
    },
    handleAvatarSuccess2(res, file) {
      this.form.proofInsuranceParticipation = res.data;
    },
    handleAvatarSuccess3(res, file) {
      this.form.attendanceEtails = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || 'image/png';
      const isLt10M = file.size / 1024 / 1024 < 10;

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG、PNG 格式!');
      }
      if (!isLt10M) {
        this.$message.error('上传图片大小不能超过 10MB!');
      }
      return isJPG && isLt10M;
    },
    close() {
      this.$emit('hideEditDialog')
    }
  }
}
</script>
